package org.dromara.web;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class CreateAndPopulateTables {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final long TWO_HOURS_IN_MILLIS = 2 * 60 * 60 * 1000;

    private static final long ONE_SECOND_IN_MILLIS = 1000L;

    public static void main(String[] args) {
//        String url = "jdbc:TAOS://localhost:6030/sensor";
//        String user = "root";
//        String password = "taosdata";
//
//        try {
//            // 连接 TDengine
//            Connection conn = DriverManager.getConnection(url, user, password);
//
//            // 创建 Statement 对象
//            Statement stmt = conn.createStatement();
////
//            // 创建超级表
//            String createSuperTableSql = "CREATE STABLE sensor (ts TIMESTAMP, `value` FLOAT) TAGS (device_id INT)";
//            stmt.execute(createSuperTableSql);
//            System.out.println("Created super table: sensor");
////            insertData2(conn, "sensor43",43);
//
////            // 创建 20000 个子表
//            for (int i = 1; i <= 21837; i++) {
//                String tableName = "sensor" + i;
//                String createTableSql = "CREATE TABLE " + tableName + " USING sensor TAGS (" + i + ")";
//                stmt.execute(createTableSql);
//                System.out.println("Created table: " + tableName);
//
//                // 插入 10000 条数据
////                insertData(conn, tableName, i);
//            }
//
//            // 关闭连接
//            stmt.close();
//            conn.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }


    public static void insertData2(Connection conn, String tableName, int deviceId) {
        String insertSql = "INSERT INTO " + tableName + " (ts, value) VALUES (?, ?)";
        try (PreparedStatement pstmt = conn.prepareStatement(insertSql)) {
            // 起始时间 2024-11-03 15:00:03
            LocalDateTime startDateTime = LocalDateTime.of(2024, 11, 3, 15, 0, 3);
            // 结束时间 2024-11-03 23:59:59
            LocalDateTime endDateTime = LocalDateTime.of(2024, 11, 3, 23, 59, 59);

            // 将 LocalDateTime 转换为 Date
            Date startDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant());

            // 计算总秒数
            long totalSeconds = ChronoUnit.SECONDS.between(startDateTime, endDateTime);

            for (int j = 0; j <= totalSeconds; j++) {
                // 计算时间戳
                long timestamp = startDate.getTime() + (j * ONE_SECOND_IN_MILLIS);
                Date ts1 = new Date(timestamp);

                // 设置参数
                pstmt.setTimestamp(1, new java.sql.Timestamp(ts1.getTime()));
                pstmt.setDouble(2, j * 0.1);

                // 执行插入
                pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void insertData(Connection conn, String tableName, int deviceId) {
        String insertSql = "INSERT INTO " + tableName + " VALUES (?, ?)";
        try (PreparedStatement pstmt = conn.prepareStatement(insertSql)) {
            Date now = new Date();
            for (int j = 1; j <= 10000; j++) {
                // 计算时间戳
                long timestamp = now.getTime() - (TWO_HOURS_IN_MILLIS - (j * 1000));
                Date ts1 = new Date(timestamp);

                // 设置参数
                pstmt.setTimestamp(1, new java.sql.Timestamp(ts1.getTime()));
                pstmt.setDouble(2, j * 0.1);

                // 执行插入
                pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
